package com.chinasoft.goodsManager.servlet;

import com.chinasoft.goodsManager.service.LoginService;
import com.chinasoft.goodsManager.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        LoginService loginService = new LoginServiceImpl();
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String verifycode = request.getParameter("vcode");
        String musername = request.getParameter("musername");
        String memail = request.getParameter("memail");
        String mpassword = request.getParameter("mpassword");
        HttpSession session = request.getSession();
        String cap = String.valueOf(session.getAttribute("cap"));
        if (cap.equals(verifycode)){
            if (loginService.findMUserName(musername)){
                if (loginService.findMEmail(memail)){
                    loginService.addNewManager(musername,memail,mpassword);
                    response.getWriter().write("注册成功");
                }else {
                    response.getWriter().write("邮箱已存在");
                }
            }else {
                response.getWriter().write("用户名已存在");
            }
        }else {
            response.getWriter().write("验证码输入错误");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
